//author:jxx
//此处是对表单的方法，组件，权限操作按钮等进行任意扩展(方法扩展可参照SellOrder.js)
let extension = {
  components: {//动态扩充组件或组件路径
    //表单header、content、footer对应位置扩充的组件
    gridHeader: '',//{ template: "<div>扩展组xx件</div>" },
    gridBody: '',
    gridFooter: '',
    //弹出框(修改、编辑、查看)header、content、footer对应位置扩充的组件
    modelHeader: '',
    modelBody: '',
    modelFooter: ''
  },
  buttons: { view: [], box: [], detail: [] },//扩展的按钮
  methods: {
    //事件扩展
    getOption (field) {
      let option;
      this.editFormOptions.forEach(x => {
        x.forEach(item => {
          if (item.field == field) {
            option = item;
          }
        });
      });
      return option;
    },
    onInit () {
      //点击单元格编辑与结束编辑(默认是点击单元格编辑，鼠标离开结束编辑)
      this.detailOptions.clickEdit = true;
      // label宽度
      this.boxOptions.labelWidth = 140
      // 弹框宽度
      this.boxOptions.width = 1000
      // 开启单选
      //;
      // 内容超出后自动换行
      this.textInline = true;
      // 不显示多选框
      //;
      // 显示序号
      //;
      this.editFormOptions[2][1].extra = {
        render: h => {
          return h(
            "div", {
            // style: { "color": "#03A9F4" }
          },
            "单位：（分钟）"
          )
        }
      }
      // 获取会员等级
      let memberLevel = this.getOption("memberLevel");
      this.http
        .post("/api/MebAccount/GetMebType", false)
        .then((res) => {
          memberLevel.data = this.treeRecursion(res.data),
          console.log(memberLevel.data,999);
      });
    },
    treeRecursion(data) {
      for (let key in data) {
        if (key === "value") {
          // 把数字类型转为字符串
          data.value = data.value.toString();
        }
        if (key == 'children' && data[key].length == 0) {
          data.children = null;
        }
        if (typeof data[key] === "object") this.treeRecursion(data[key]);
      }
      return data;
    },    
    
    rowClick ({ row, column, event }) { //查询界面table点击行选中当前行
      this.$refs.table.$refs.table.toggleRowSelection(row);
      return true;
    },
    modelOpenAfter (row) {
      if (this.currentAction == "update") {
        // 将开始日期 和 结束日期合并为起止日期
        if (row.parkOpenStartTime && row.parkOpenEndTime) {
          let startTime = row.parkOpenStartTime.split(" ")[1]
          let endTime = row.parkOpenEndTime.split(" ")[1]
          this.editFormFields.timeContainer = [startTime, endTime]
        }
      }
    },
    modelOpenBefore (row) {
      if (this.currentAction == "Add") {
      } else {
        if (row.cardType) {
          let arr = row.cardType.split(',')
          arr.forEach(v=>{
            this.editFormFields.cardType.push(Number(v))
          })
        }

        if (row.memberLevel) {
          // 会员等级
          this.editFormFields.memberLevel = row.memberLevel.split(',')
        }
      }
    },
    addBefore (formData) { //新建保存前formData为对象，包括明细表
      return this.veriFromTable(formData)
    },
    updateBefore (formData) { //编辑保存前formData为对象，包括明细表、删除行的Id
      return this.veriFromTable(formData)
    },
    // 保存前编辑 
    veriFromTable (formData) {
      // 分割园区开放时间
      let timeContainer = formData.mainData.timeContainer.split(",")
      if (timeContainer.length > 0) {
        formData.mainData.parkOpenStartTime = timeContainer[0]
        formData.mainData.parkOpenEndTime = timeContainer[1]
      }
      // 从表 闸机名称 闸机编码 必填
      let alphanumeric = /^\w+$/ //数字  字母
      for (let index = 0; index < formData.detailData.length; index++) {
        const element = formData.detailData[index];
        if (!element.gateName) {
          this.$Message.error("请输入闸机名称！")
          return false
        } else if (!element.gateCode) {
          this.$Message.error("请输入闸机编码！")
          return false
        } else if (!alphanumeric.test(element.gateCode)) {
          this.$Message.error("闸机编码只允许数字与字母！")
          return false
        }
      }
      return true
    },
  }
};
export default extension;